import { message } from 'antd';
export class WebTool {
    static getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }

    static getNowTimeStr() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        let dateStr = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
        return dateStr;
    }

    static success(msg) {
        message.success(msg || 'success');
    }

    static info(msg) {
        message.info(msg || 'info');
    }

    static error(msg) {
        message.error(msg || 'error');
    }
    static get(url, callback) {
        var req = new Request(url, { method: 'GET', cache: 'reload' });

        this.fetch(req, callback);
    }
    static post(url, data, callback) {
        if(typeof data == "object"){
            data = JSON.stringify(data);
        }

        var req = new Request(url, {
            method: 'POST', body: data, cache: 'reload', headers: {
                "Content-Type": "application/json"
            }
        });
        this.fetch(req, callback);
    }
    
    static fetch(req, callback){
        fetch(req).then((response) => {
            response.json().then(json => {
                if (json.success !== true) {
                    this.error(json.msg || 'some error');
                    return;
                }
                callback && callback(json.data);
            });
        })
    }
};

// module.exports = object;